/**
 * 弱引用遭到gc就会回收
 */
package com.janko.springbootbase.juc;

import com.janko.springbootbase.basic.Hello;

import java.lang.ref.WeakReference;

public class RF_02WeakReference {
    public static void main(String[] args) {
        WeakReference<Hello> m = new WeakReference<>(new Hello());

        System.out.println(m.get());
        System.gc();
        System.out.println(m.get());


        //ThreadLocal 就是弱引用weakReference 的实现
        ThreadLocal<Hello> tl = new ThreadLocal<>();
        tl.set(new Hello());
        /**如果不进行remove的话   会造成内存泄露，原因是：ThreadLocal被回收后，里边ThreadLocalMap中entry对应的可以值是null
         * 这个时候,需要长期累积，依然会造成内存堆积，溢出的可能
         */
        tl.remove();

    }
}

